package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("AuthorityDAO")
@Transactional
public class AuthorityDAOImpl extends AbstractJpaDao<Authority> implements AuthorityDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Authority.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public AuthorityDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Authority findAuthorityById(Integer id) throws DataAccessException {

		return findAuthorityById(id, -1, -1);
	}

	@Transactional
	public Authority findAuthorityById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAuthorityById", startResult, maxRows, id);
			return (Authority) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Authority> findAuthorityByName(String name) throws DataAccessException {

		return findAuthorityByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Authority> findAuthorityByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAuthorityByName", startResult, maxRows, name);
		return new LinkedHashSet<Authority>(query.getResultList());
	}

	@Transactional
	public Set<Authority> findAuthorityByNameContaining(String name) throws DataAccessException {

		return findAuthorityByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Authority> findAuthorityByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAuthorityByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Authority>(query.getResultList());
	}

	@Transactional
	public Set<Authority> findAuthorityByDescription(String description) throws DataAccessException {

		return findAuthorityByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Authority> findAuthorityByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAuthorityByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Authority>(query.getResultList());
	}

	@Transactional
	public Set<Authority> findAuthorityByDescriptionContaining(String description) throws DataAccessException {

		return findAuthorityByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Authority> findAuthorityByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAuthorityByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Authority>(query.getResultList());
	}

	@Transactional
	public Authority findAuthorityByPrimaryKey(Integer id) throws DataAccessException {
		return findAuthorityByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Authority findAuthorityByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAuthorityByPrimaryKey", startResult, maxRows, id);
			return (Authority) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Authority> findAuthoritysByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAuthoritysByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Authority> findAllAuthoritys() throws DataAccessException {

		return findAllAuthoritys(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Authority> findAllAuthoritys(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAuthoritys", startResult, maxRows);
		return new LinkedHashSet<Authority>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Authority> findAuthorityByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Authority>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Authority> findAuthoritysByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Authority.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Authority> authoritys = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return authoritys;
	}

	@Transactional
	public Authority findAuthorityByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findAuthorityByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Authority findAuthorityByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findAuthorityByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Authority) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Authority entity) {
		return true;
	}
}
